package io.summer.gadget.channel.core.impl;

import io.summer.basis.excepts.AppException;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.function.Function;

public class ChannelConsumer {
  /**
   * 只能有1个入参和1个出参
   */
  public final static int HANDLER_SIG_TYPES = 2;

  public final static int INPUT_TYPE = 0;
  public final static int RETURN_TYPE = 1;

  public ChannelConsumer(Type[] types, Function<?, ?> consumer) {
    this.listenerTypes = types;
    this.consumer = consumer;
    if (!check()) {
      throw new AppException("error ChannelConsumer signature");
    }
  }

  private final Type[] listenerTypes;

  @SuppressWarnings("rawtypes")
  public final Function consumer;

  public Type getInputType() {
    return listenerTypes[INPUT_TYPE];
  }

  public Type getReturnType() {
    return listenerTypes[RETURN_TYPE];
  }

  public boolean check() {
    return listenerTypes.length == HANDLER_SIG_TYPES && consumer != null;
  }

  @Override
  public String toString() {
    return Arrays.toString(listenerTypes) + "|" + consumer.getClass().getName();
  }
}
